package com.kdtech.analyse.blog;
import com.kdtech.crawler.CrawlHTML;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 中华博客
 * @author Persh
 *
 */
public class ChinaBlogAnalyse implements AnalyseNews{


	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://.*.blog.china.com/[0-9]{6}/[0-9]*.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String html = urlMeta.getHtml();
		if (html== null) {
		}
		NewsMeta blog = new NewsMeta();
		blog.setUrl(url);
		String title = null;
		String content = null;
		String author=null;
		Long date = null;
		Integer clickNum=0;
		Integer commentNum=0;
		Integer deliverNum=0;
		Document doc = Jsoup.parse(html);
		author = doc.select("html body div#main div#blog_body div.leftpane div.intro table tbody tr td.txt").text();
		if(author.endsWith("博客")){
			author = author.substring(0,author.length()-2);
		}
		blog.setAuthor(author);
		title =doc.select("div#show_hide_title02").text();
		if(title==null || title.trim().length()==0){
		}
		content = HtmlCleaner.getContentHtml(url, doc.select("div.txt"));
		String tempdate=doc.select("div.time").text();
		if(tempdate==null || tempdate.trim().length()==0){
		}
		date = DateUtils.matchDate(tempdate);
		blog.setTitle(StringUtils.trimSpace(title));
		blog.setContent(content);
		blog.setDate(date);

		try {
			String scripHtml = html.substring(html.indexOf("<!-- comment start -->"));
			scripHtml = scripHtml.substring(scripHtml.indexOf("<script>"), scripHtml.indexOf("</script>"));
			String commentStr = scripHtml.substring(scripHtml.indexOf("评论(")+"评论(".length(), scripHtml.indexOf(")\""));
			commentNum = Integer.parseInt(commentStr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
		blog.setClickNum(clickNum);
		blog.setCommentNum(commentNum);
		//blog.setDeliverNum(deliverNum);
		blog.setUpdateUrl(url);
		return blog;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String htmltxt = responseToURL.getHtml();
					Integer clickNum=0;
					Integer commentNum=0;
					Integer deliverNum=0;
					try {
						String scripHtml = htmltxt.substring(htmltxt.indexOf("<!-- comment start -->"));
						scripHtml = scripHtml.substring(scripHtml.indexOf("<script>"), scripHtml.indexOf("</script>"));
						String commentStr = scripHtml.substring(scripHtml.indexOf("评论(")+"评论(".length(), scripHtml.indexOf(")\""));
						commentNum = Integer.parseInt(commentStr);
					} catch (Exception e) {
					}
					meta.setClickNum(clickNum);
					meta.setCommentNum(commentNum);
					meta.setCommentNum(deliverNum);
					return meta;
				}
			}
		}
		return null;
	}





	public static void main(String[] args) {
		String url = "http://wangyoucao.blog.china.com/201405/13057350.html";
		ChinaBlogAnalyse test = new ChinaBlogAnalyse();
		if(test.isDetailPage(url)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = test.parserHtml(responseToURL);
			System.out.println(parserHtml);
		}else{
			System.out.println("不符合规则");
		}
	}

	
}
